<?php
namespace Home\Controller;
use Think\Controller;

class IndexController extends FrontController {

	public function call($id1='', $id2='') {
		if (($id1 == '') || ($id2 == '')) {
			$this->success('请先登录!', '/Home/User/index');
		}
		$phone1 = ''; //主叫,先接电话,卖家
		$phone2 = ''; //被叫,后接电话,用户
		$user1 = D('User')->find($id1);
		$user2 = D('User')->find($id2);
		//创建呼叫记录
		unset($data);
		$data['calling_user_id'] = $id1;
		$data['calling_user_phone'] = $user1['username'];
		$data['called_user_id'] = $id2;
		$data['called_user_phone'] = $user2['username'];
		$model = D('CallRecord');
		if (!$model->create($data)) {
			$this->error($model->getError());
			return false;
		}
		$model->add();
		//根据用户id获取用户电话，并确定主被叫
		if ($user1['type'] == $user2['type']) {
			$phone1 = $user1['username'];
			$phone2 = $user2['username'];
		} else if ($user1['type'] != '用户') {
			$phone1 = $user1['username'];
			$phone2 = $user2['username'];
		} else if ($user2['type'] != '用户') {
			$phone1 = $user2['username'];
			$phone2 = $user1['username'];
		}
		//发起呼叫
		$soap = new \SoapClient('http://139.129.223.47:8921/FBCCFSWebServer/services/AgentServer?wsdl');
	/*$soap = new \SoapClient(null,array('location'=>'http://139.129.223.47:8921/FBCCFSWebServer/services/AgentServer',uri => 'member_sync.php'));*/
		$result = $soap->phonebrphone(array(
			'in0' => $phone1,
			'in1' => $phone2,
			'in2' => 'agent',
			'in3' => 'freebridge',
			'in4' => 8000,
		));
		$this->error('呼叫系统已启动，您可能会接到陌生号码的来电。如未接到来电可能是对方忙线，请再次点击呼叫，谢谢。');
	}

	public function index() {
		//sui
		$system = D('System')->find(1);
		$this->assign('title', $system['name']);
		$sui = [
			'index' => 'active',
			'return' => false,
		];
		$this->assign('sui', $sui);
		//滑动图
		$ad = D('Ad')->select();
		$this->assign('ad', $ad);
        $whereStr = 'already_num != total_num AND transaction_type="发起" AND state="众筹中"';
        //热门目的地
        $destinationFunding = D('Funding')->where($whereStr)->order('already_num/total_num desc')->limit(4)->select();
        $this->assign('destinationFunding', $destinationFunding);
        //热门筹单
        $hotFunding = D('Funding')->where($whereStr)->order('already_num/total_num desc')->limit(4)->select();
        $this->assign('hotFunding', $hotFunding);
        //天天优惠
        $discountFunding = D('Funding')->where(['is_budget'=>'是'])->where($whereStr)->order('id desc')->limit(4)->select();
        $this->assign('discountFunding', $discountFunding);
		$this->display();
	}

	public function customized() {
		//sui
		$this->assign('title', '定制');
		$sui = [
			'customized' => 'active',
			'return' => true,
		];
		$this->assign('sui', $sui);

		$this->display();
	}

	public function information() {
		//sui
		$this->assign('title', '信息');
		$sui = [
			'information' => 'active',
			'return' => true,
		];
		$this->assign('sui', $sui);

		$this->display();
	}

	public function introduce() {
		//sui
		$this->assign('title', '使用说明');
		$sui = [
			'customized' => 'active',
			'return' => true,
		];
		$this->assign('sui', $sui);
		$this->display();
	}

	public function contract() {
		//sui
		$this->assign('title', '联系我们');
		$sui = [
			'customized' => 'active',
			'return' => true,
		];
		$this->assign('sui', $sui);
		$this->display();
	}


	public function h5link() {
		//更新h5_link
		
		//跳转到指定位置
		if ($_GET['user_type'] == 'shop') {
			D('Funding')->where(['id'=>$_GET['funding_id']])->data(['h5_link'=>$_GET['h5_link']])->save();
			$this->success('操作成功!', U('Shop/Funding/list'));
		} else if ($_GET['user_type'] == 'club') {
			D('Funding')->where(['id'=>$_GET['funding_id']])->data(['h5_link'=>$_GET['h5_link']])->save();
			$this->success('操作成功!', U('Club/Funding/list'));
		} else if ($_GET['user_type'] == 'guider') {
			//更新导游的h5_link
			D('UserGuider')->where(['id'=>$_GET['funding_id']])->data(['h5_link'=>$_GET['h5_link']])->save();
			$this->success('操作成功!', U('/Guider/UserGuider/update/id/'.$_GET['funding_id']));
		}
	}
}
